package autoteam.tables;

import javax.swing.table.*;

import autoteam.Child;
import autoteam.League;

public class WeightEditorTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	Double[] vals = null;
	static String s = "Skill";
	Child[] children = null;
	League doc = null;

	public WeightEditorTableModel(League _doc) {
		doc = _doc;
		vals = new Double[_doc.getNumberOfSkillFields()];
		children = new Child[_doc.getNumberOfChildren()];
		setChildren(_doc);
	}

	public void setChildren(League _doc) {
		doc = _doc;
		for (int i = 0; i < children.length; i++)
			children[i] = _doc.getChild(i);
		for (int i = 0; i < vals.length; i++)
			vals[i] = children[0].getWeight(i);
	}

	public void transfer() {
		for (int i = 0; i < children.length; i++)
			children[i].totalSkill = 0;

		for (int i = 0; i < children.length; i++) {
			for (int j = 0; j < vals.length; j++) {
				if (children[i].weight[j] != vals[j]) {
					if ((children[i].weight[j] == null) || (vals[j] == null)
							|| (!children[i].weight[j].equals(vals[j]))) {
						doc.setDirty();
						children[i].weight[j] = vals[j];
					}
				}
			}

			for (int j = 0; j < vals.length; j++)
				children[i].totalSkill += children[i].skill[j].doubleValue()
						* vals[j].doubleValue();
		}
	}

	public int getColumnCount() {
		return 2;
	}

	public int getRowCount() {
		return vals.length;
	}

	public String getColumnName(int col) {
		return (col == 0 ? "Skill" : "Weight");
	}

	public Object getValueAt(int row, int col) {
		if (col == 0) {
			return doc.getSkillName(row);
		}
		return vals[row];
	}

	public Class getColumnClass(int c) {
		return (c == 0 ? getColumnName(0).getClass() : Double.class);
	}

	public boolean isCellEditable(int row, int col) {
		return (col == 1);
	}

	public void setValueAt(Object value, int row, int col) {
		vals[row] = (Double) value;
		System.out.println("Weights[" + row + "] = " + vals[row]);
	}
}
